package com.liyuxiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liyuxiang.constants.SystemConstants;
import com.liyuxiang.mapper.UserMapper;
import com.liyuxiang.pojo.entity.LoginUser;
import com.liyuxiang.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    //注入查询用户的mapper
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName, username));
        Assert.notNull(user, "用户名不存在");
        Assert.isTrue(Integer.parseInt(user.getStatus()) == SystemConstants.USER_STATUS_NORMAL, "账号已被停用，请联系管理员");
        //正常，返回user
        //TODO 查询权限信息封装
        return new LoginUser(user);
    }
}
